package com.SH.service.impl;

import com.SH.mapper.UserMapper;
import com.SH.pojo.User;
import com.SH.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import javax.mail.MessagingException;
import java.util.List;

@Service
public class UserServiceImpl implements UserService{

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private MailService mailService;

    @Override
    public User getUserByName(String username) {
        User user = userMapper.getUserByName(username);
        return user;
    }

    @Async
    public Boolean addUser(User user) {
        Boolean sign = userMapper.addUser(user);
        if (sign==true){
            try {
                mailService.sendMail(user.getEmail(),user.getUsername());
            } catch (MessagingException e) {
                e.printStackTrace();
            }
        }
        return sign;
    }

    @Override
    public List<User> getUsers() {
        List<User> users = userMapper.getUsers();
        return users;
    }

    @Override
    public User getUserById(Integer id) {
        User user = userMapper.getUserById(id);
        return user;
    }

    @Override
    public Boolean updateUser(User user) {
        Boolean aBoolean = userMapper.updateUser(user);
        return aBoolean;
    }

    @Override
    public List<String> getRoles() {
        List<String> roles = userMapper.getRoles();
        return roles;
    }

    @Override
    public Boolean deleteUser(Integer id) {
        return userMapper.deleteUser(id);
    }
}
